﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;

public partial class ProductDetail : System.Web.UI.Page
{
    private ProductManager pm = new ProductManager();
    private CategoryManager cm = new CategoryManager();
    private SupplierManager sm = new SupplierManager();
    public Product pr = new Product();
    public string sCategory = "";
    public string sSupplier = "";
    public string isStored = "Unavailable";
    public string ImageLink;
    private ProductManager man = new ProductManager();
    private ShoppingCard card = new ShoppingCard();

    public decimal totalPrice = 0;
    public string totalQuantity = "0";

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["cart"] != null)
        {
            BLShoppingCart cart = (BLShoppingCart)Session["cart"];
            totalPrice = cart.TotalPayment();
            totalQuantity = cart.TotalQuantity().ToString();
        }

        if (Request.QueryString["pid"] != null)
        {
            int pid = Int32.Parse(Request.QueryString["pid"]);
            pr = pm.GetProductById(pid);
            Image2.ImageUrl = pr.imageLink;
            foreach (Category ca in cm.AllCategory())
            {
                if (ca.CategoryID == pr.CategoryID)
                {
                    sCategory = ca.CategoryName;
                }
            }

            foreach (Supplier sp in sm.AllSupplier())
            {
                if (sp.SupplierID == pr.SupplierID)
                {
                    sSupplier = sp.CompanyName;
                }
            }

            if (pr.Quantity > 0)
            {
                isStored = "Available";
            }

            ImageLink = pr.imageLink.Substring(2);
        }

        
    }
    protected void btnAddCart_Click(object sender, ImageClickEventArgs e)
    {
        card.Cards = GetCart();
        card.AddProduct(man.GetProductById(pr.ProductID));
        Session["Cart"] = card.Cards;
        Response.Redirect("ShoppingCart.aspx");
    }

    private Dictionary<Product, int> GetCart()
    {
        if (Session["Cart"] == null)
            Session.Add("Cart", new Dictionary<Product, int>());
        return (Dictionary<Product, int>)Session["Cart"];
    }
}